﻿/* ***** BEGIN LICENSE BLOCK *****
 * Version: MPL 1.1
 *
 * The contents of this file are subject to the Mozilla Public License
 * Version 1.1 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
 * License for the specific language governing rights and limitations
 * under the License.
 *
 * The Original Code is tyzoh.jp code.
 *
 * The Initial Developer of the Original Code is Kazutaka Kurihara.
 * Portions created by the Initial Developer are Copyright (C) 2007
 * the Initial Developer. All Rights Reserved.
 *
 * Contributor(s):
 *
 * ***** END LICENSE BLOCK ***** */
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using Microsoft.Ink;
using System.Windows.Forms;

namespace Qurihara
{
	/// <summary>
	/// InkView の概要の説明です。
	/// </summary>
	public class InkView : View
	{
		public InkView(Size s, InkCollector ic,Control _parent) :base(s)
		{
			inkCollector = ic;
			parent = _parent;
			SetRatio();
			//inkCollector.NewPackets +=new InkCollectorNewPacketsEventHandler(inkCollector_NewPackets);
			//inkCollector.Stroke += new InkCollectorStrokeEventHandler(inkCollector_Stroke);

			//wm = new Matrix();
			//inkCollector.Renderer.GetViewTransform(ref wm);
		}

		protected Control parent;
		protected Rectangle inkViewRect;
		protected override void SetRatio()
		{
			base.SetRatio ();

			if(inkCollector != null)
			{
				Point ptTL, ptBR;
				ptTL = ptBR = viewRect.Location;
				ptBR += viewRect.Size;
				// Find the edges of the bounding box in pixel space
				//inkCollector.Renderer.PixelToInkSpace(inkCollector.Handle,ref ptTL);
				//inkCollector.Renderer.PixelToInkSpace(inkCollector.Handle,ref ptBR);
				Graphics g = parent.CreateGraphics();
				inkCollector.Renderer.PixelToInkSpace(g,ref ptTL);
				inkCollector.Renderer.PixelToInkSpace(g,ref ptBR);
				g.Dispose();
				// Copy the out parameters
				inkViewRect.X = ptTL.X;
				inkViewRect.Y = ptTL.Y;
				inkViewRect.Width = ptBR.X - ptTL.X;
				inkViewRect.Height = ptBR.Y - ptTL.Y;
			}
		}

		protected InkCollector inkCollector;
		public InkCollector InkCollector
		{
			get{return inkCollector;}
			set{InkCollector = value;}
		}
/*
		protected Stroke strokeBeingDrawn = null;
		public Stroke StrokeBeingDrawn
		{
			get{return strokeBeingDrawn;}
		}
		private void inkCollector_NewPackets(object sender, InkCollectorNewPacketsEventArgs e)
		{
			strokeBeingDrawn = e.Stroke;
		}
		private void inkCollector_Stroke(object sender, InkCollectorStrokeEventArgs e)
		{
			strokeBeingDrawn = null;
		}
*/
		protected Matrix m;
		protected Matrix wm2;

		//protected bool flag = true;
		public void InkRendererTransform()
		{
			/*
			if (!flag)
			{
				inkCollector.Renderer.SetViewTransform(wm);
				wm.Dispose();
			}
			flag = false;
*/
			m = new Matrix();
			wm2 = new Matrix();

			// 現在のMatrixを保存
			inkCollector.Renderer.GetViewTransform(ref wm2);

			//m.Translate(-inkViewRect.X,-inkViewRect.Y);

			// 倍率変換
			m.Scale(viewToScreenRatio.X,viewToScreenRatio.Y);
			m.Translate(-inkViewRect.X,-inkViewRect.Y);

			inkCollector.Renderer.SetViewTransform(m);
			
			/*
			//if (inkCollector.CollectingInk)
			//{
				if (strokeBeingDrawn != null)
				{
					m.Invert();
					strokeBeingDrawn.Transform(m);
				}
			//}
*/			
			m.Dispose();
		}
		public void InkRendererResume()
		{
			//要は単位行列をかければよいようである。
			//wm.Dispose();
			//wm = new Matrix();
			inkCollector.Renderer.SetViewTransform(wm2);
			wm2.Dispose();
			
		}

		public void InkScreenToInkView(ref Stroke s)
		{
			Point [] ps = s.GetPoints();
			InkScreenToInkView(ref ps);
			s.SetPoints(ps);
		}
		public void InkScreenToInkView(ref Point p)
		{
			p.X = inkViewRect.X + (int)(screenToViewRatio.X * p.X);
			p.Y = inkViewRect.Y + (int)(screenToViewRatio.Y * p.Y);
		}
		public void InkScreenToInkView(ref Point [] ps)
		{
			for(int i=0;i<ps.Length;i++) InkScreenToInkView(ref ps[i]);
		}
		public void InkScreenToInkView(ref Rectangle r)
		{
			Point tl = new Point(r.Left,r.Top);
			Point br = new Point(r.Right,r.Bottom);
			InkScreenToInkView(ref tl);
			InkScreenToInkView(ref br);
			r.Location = tl;
			r.Width = br.X -tl.X;
			r.Height = br.Y - tl.Y;
		}
		public void InkViewToInkScreen(ref Point p)
		{
			p.X = (int)(viewToScreenRatio.X * (p.X - inkViewRect.X));
			p.Y = (int)(viewToScreenRatio.Y * (p.Y - inkViewRect.Y));
		}
		public void InkViewToInkScreen(ref Point [] ps)
		{
			for(int i=0;i<ps.Length;i++) InkViewToInkScreen(ref ps[i]);
		}
		public void InkViewToInkScreen(ref Rectangle r)
		{
			Point tl = new Point(r.Left,r.Top);
			Point br = new Point(r.Right,r.Bottom);
			InkViewToInkScreen(ref tl);
			InkViewToInkScreen(ref br);
			r.Location = tl;
			r.Width = br.X -tl.X;
			r.Height = br.Y - tl.Y;
		}

	}
}
